home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / tls / tls088.hpux.Z / tls088.hpux / lib / vtcl / tests / hlist.tcl < prev    next >
Encoding:
Text File  |  1995-07-20  |  10.2 KB  |  443 lines

  1. # CVS $Id: hlist.tcl,v 1.3 1995/04/29 16:07:24 zibi Exp $
  2. #
  3. # This creates a drawn list with some extra data to maintain
  4. # a hierarch of records
  5. #
  6. proc VxExpandoList {dlist addProc fmt args} {
  7.     
  8.     set dlist [eval VtDrawnList $dlist $args]
  9.  
  10.     set fmt [linsert $fmt 0 {DATA}]
  11.  
  12.     # Associate format and count to this list
  13.     # 
  14.     VxSetVar $dlist format $fmt
  15.     VxSetVar $dlist count  0
  16.  
  17.     return $dlist
  18. }
  19.  
  20. #
  21. # This deletes all positions
  22. # below pos that have a greater level.
  23. #
  24. proc VxExpandoCollapse {dlist pos} {
  25.     set count [VxGetVar $dlist count]
  26.  
  27.     if {$pos >= $count} {
  28.     return
  29.     }
  30.  
  31.     VxExpandoDeleteItem $dlist $pos on
  32. }
  33.  
  34. proc VxExpandoGetLevel {dlist pos} {
  35.  
  36.     set record [VtDrawnListGetItem $dlist -position $pos]
  37.     set record [lindex $record 0]
  38.  
  39.     set recordLevel [lindex $record 0]
  40.     return $recordLevel
  41. }
  42.  
  43. proc VxExpandoGetIcon {dlist pos} {
  44.  
  45.     set record [VtDrawnListGetItem $dlist -position $pos]
  46.     set record [lindex $record 0]
  47.  
  48.     set icons [lindex $record 1]
  49.     set end [llength $icons]
  50.     return [lrange $icons $end end]
  51. }
  52.  
  53. proc VxExpandoSetIcon {dlist pos icon} {
  54.     set record [VtDrawnListGetItem $dlist -position $pos]
  55.     set record [lindex $record 0]
  56.  
  57.     set icons [lindex $record 1]
  58.     set icons [lreplace $icons end end $icon]
  59.     set record [lreplace $record 1 1 $icons]
  60.     VtDrawnListSetItem $dlist -position $pos -fieldList $record
  61. }
  62.  
  63. proc VxExpandoGetRecord {dlist pos level icon rec} {
  64.     
  65.     set record [VtDrawnListGetItem $dlist -position $pos]
  66.     set record [lindex $record 0]
  67.  
  68.     set icons [lindex $record 1]
  69.  
  70.     upvar 1 $icon rIcon $rec rRecord $level rLevel
  71.  
  72.     set rLevel  [lindex $record  0]
  73.     set end     [llength $icons]
  74.     set rIcon   [lrange $icons $end end]
  75.     set rRecord [lrange $record 2 end]
  76. }
  77.  
  78. #
  79. # The next two functions return true if the icon has a
  80. # upwards or downward connector component
  81. #
  82. proc hasDown {icon} {
  83.     switch $icon {
  84.     CONNECT_I -
  85.     CONNECT_T  {
  86.         return 1
  87.     }
  88.     default {
  89.         return 0
  90.     }
  91.     }
  92. }
  93.  
  94. proc hasUp {icon} {
  95.     switch $icon {
  96.     CONNECT_L -
  97.     CONNECT_I -
  98.     CONNECT_T  {
  99.         return 1
  100.     }
  101.     default {
  102.         return 0
  103.     }
  104.     }
  105. }
  106.  
  107. #
  108. # Sets the icon connector for the n'th record in the dlist
  109. # dlist        - DrawnList
  110. # n        - position of record in the drawnlist
  111. # col        - position of icon in the record.  The icons are in the second
  112. #          elements of the record.  
  113. # connector    - connector to set to.
  114. #
  115. proc setIconConnector {dlist n col connector} {
  116.     set rec [VtDrawnListGetItem $dlist -position $n]
  117.     set rec [lindex $rec 0]
  118.  
  119.     set icons [lindex $rec 1]
  120.     if {$col < [llength $icons]} {
  121.     set icons [lreplace $icons $col $col $connector]
  122.     }
  123.  
  124.     set rec [lreplace $rec 1 1 $icons]
  125.  
  126.     VtDrawnListSetItem $dlist -position $n -fieldList $rec
  127. }
  128.  
  129. proc getIconConnector {dlist n col} {
  130.     set rec [VtDrawnListGetItem $dlist -position $n]
  131.     set rec [lindex $rec 0]
  132.  
  133.     set icons [lindex $rec 1]
  134.     if {$col < [llength $icons]} {
  135.     return [lindex $icons $col]
  136.     } else {
  137.     return ""
  138.     }
  139. }
  140.  
  141. #
  142. # "Patches" all the connectors to the left of the current
  143. # record up the next record that has the same level.
  144. #
  145. # dlist        - drawnlist
  146. # pos        - record position to start the patch
  147. # level        - level of the heirarchy
  148. # cmd        - "ADD" or "SUB" to add or subtract a vertical component.
  149. #
  150. proc patchConnectors {dlist pos level cmd} {
  151.     set indx [expr $level - 1]
  152.     while {1} {
  153.     set icon [getIconConnector $dlist $pos $indx]
  154.     set rLevel [VxExpandoGetLevel $dlist $pos]
  155.     
  156.     if {$cmd == "ADD"} {
  157.         if {$icon == "NO_ICON"} {
  158.         set icon CONNECT_I
  159.         } elseif {$icon == "CONNECT_L"} {
  160.         set icon CONNECT_T
  161.         } 
  162.     } else {
  163.         echo "icon $icon pos $pos"
  164.         echo "rLevel $rLevel $level "
  165.  
  166.         if {$icon == "CONNECT_I"} {
  167.         set icon NO_ICON
  168.         } elseif {$icon == "CONNECT_T"} {
  169.         set icon CONNECT_L
  170.         }
  171.     }
  172.  
  173.     setIconConnector $dlist $pos $indx $icon
  174.     
  175.     incr pos -1
  176.     if {$rLevel <= $level} {break}
  177.     }
  178. }
  179.  
  180. #
  181. # This proc analyzes the icon connectors. It gets the connector information
  182. # for the current position and will generate a connector list based
  183. # on that position.
  184. proc _analyzeConnect {dlist pos level icon} {
  185.     if {$level == 0} { return $icon }
  186.  
  187.     set count [VxGetVar $dlist count]
  188.     if {$pos == 0} {set pos $count}
  189.  
  190.     # Get the record above and below the one we want to insert
  191.     set aboveRec [VtDrawnListGetItem $dlist -position [expr $pos - 1] ]
  192.     set aboveRec [lindex $aboveRec 0]
  193.  
  194.     if {$pos < $count} {
  195.         set belowRec [VtDrawnListGetItem $dlist -position [expr $pos]]
  196.     set belowRec [lindex $belowRec 0]
  197.     } else {
  198.     set belowRec ""
  199.     }
  200.  
  201.     set aboveIcons [lindex $aboveRec 1]
  202.     set belowIcons [lindex $belowRec 1]
  203.     set currentIcons ""
  204.  
  205.     # analyze all position up to the one before the icon
  206.     set endLevel [expr $level - 1]
  207.     for {set i 0} {$i < $endLevel} {incr i} {
  208.     set abvIcon [lindex $aboveIcons $i]
  209.     set blwIcon [lindex $belowIcons $i]
  210.  
  211.     if {[hasDown $abvIcon] && [hasUp $blwIcon]} {
  212.         lappend currentIcons CONNECT_I
  213.     } else {
  214.         lappend currentIcons NO_ICON
  215.     }
  216.     }    
  217.     
  218.     # analyze the connector position just before the icon
  219.     set abvIcon [lindex $aboveIcons $endLevel]
  220.     set blwIcon [lindex $belowIcons $endLevel]
  221. # echo $abvIcon .. $blwIcon ++ $aboveRec
  222.  
  223.    if {[hasUp $blwIcon]} {
  224.     lappend currentIcons CONNECT_T
  225.     } else {
  226.     lappend currentIcons CONNECT_L
  227.     }
  228.     
  229.     lappend currentIcons 4 $icon
  230.  
  231.     return $currentIcons
  232. }
  233.  
  234. proc isConnector {i} {
  235.     switch $i {
  236.     NO_ICON   -
  237.     CONNECT_L -
  238.     CONNECT_I -
  239.     CONNECT_T  {
  240.         return 1
  241.     }
  242.     default {
  243.         return 0
  244.     }
  245.     }
  246. }
  247.       
  248. #
  249. # dlist     - drawnlist to add to
  250. # pos        - position to add to
  251. # level        - record level
  252. # icon        - pixmap to use for this record
  253. # data        - formatList data
  254. proc VxExpandoAddItem {dlist pos level icon data} {
  255.     #
  256.     # Get dlist info
  257.     set count [VxGetVar $dlist count]
  258.     set fmt   [VxGetVar $dlist format]
  259.  
  260.     set fmt [_genFormat $fmt $level]
  261.     set connectors [_analyzeConnect $dlist $pos $level $icon]
  262.  
  263.     set data [linsert $data 0 $level $connectors]
  264.  
  265.     eval [list VtDrawnListAddItem $dlist -formatList $fmt -fieldList $data \
  266.              -position $pos ]
  267.  
  268.     if {$pos == 0} {
  269.     set pos $count
  270.     } else {
  271.     incr pos -1
  272.     }
  273.  
  274.     # Now fix all the connectors between the current one and all
  275.     # above 
  276.     if {$level > 0} {
  277.     patchConnectors $dlist $pos $level ADD
  278.     }
  279.  
  280.     incr count 1
  281.  
  282.     VxSetVar $dlist count $count
  283. }
  284.  
  285. #
  286. # This deletes an items in the hlist.  If you delete a container record
  287. # it'll delete all items in the container.
  288. #
  289. proc VxExpandoDeleteItem {dlist pos {collaspe off} } {
  290.     set count [VxGetVar $dlist count]
  291.     set startLevel [VxExpandoGetLevel $dlist $pos]
  292.  
  293.     if {[getIconConnector $dlist $pos [expr $startLevel-1]] == "CONNECT_L"} {
  294.     set patch on
  295.     } else {
  296.     set patch off
  297.     }
  298.  
  299.     if {$collaspe} {incr pos}
  300.  
  301.     while {$pos <= $count} {
  302.     VtDrawnListDeleteItem $dlist -position $pos
  303.  
  304.     incr count -1
  305.     if {$count == 0 || $pos > $count} {break}
  306.  
  307.     set level [VxExpandoGetLevel $dlist $pos]
  308.  
  309.     if {$level <= $startLevel} {break}
  310.     }
  311.  
  312.     if {$patch} {
  313.     incr pos -1
  314.     patchConnectors $dlist $pos $startLevel SUB
  315.     }
  316.  
  317.     # Select the next item
  318.     if {$count} {
  319.     if {$pos > $count} { set pos 1 }
  320.     VtDrawnListSelectItem $dlist -position $pos
  321.     }
  322.  
  323.     VxSetVar $dlist count $count
  324. }
  325.  
  326. #
  327. # This sets the width of the icon connector column
  328. proc _genFormat {userFormat level} {
  329.  
  330.     set fList [list ICON [expr $level+2] ]
  331.  
  332.     set fmt [linsert $userFormat 1 $fList]
  333.     return $fmt
  334. }    
  335.  
  336. # ----------------------------------------------------------------------
  337.  
  338. proc addBogus {dlist pos level} {
  339.  
  340.     incr pos
  341.  
  342.     set bigStr "WWWWWWWWAAAAAAAAAAWWAWSA01234566"
  343.     VxExpandoAddItem $dlist $pos $level 0 { "New 1.9" "hide" "hide"}; incr pos
  344.     VxExpandoAddItem $dlist $pos $level 2 { "New 2.9" "hide" "hide"}; incr pos
  345.     VxExpandoAddItem $dlist $pos $level 0 $bigStr
  346. }
  347.  
  348. #
  349. # This routine adds three icons underneath the current directory
  350. #
  351. proc addCB {cbs} {
  352.     set dlog [keylget cbs dialog]
  353.     
  354.     set dlist [VxGetVar $dlog dlist]
  355.  
  356.     set pos   [VtDrawnListGetSelectedItem $dlist -byPositionList]
  357.  
  358.     VxExpandoGetRecord $dlist $pos level icon record
  359.  
  360.     if {$icon == 0} {
  361.     VxExpandoSetIcon $dlist $pos 1
  362.     addBogus $dlist $pos [expr $level + 1]
  363.     }
  364. }
  365.  
  366. proc deleteCB {cbs} {
  367.     set dlog [keylget cbs dialog]
  368.  
  369.     set dlist [VxGetVar $dlog dlist]
  370.  
  371.  
  372.     set pos [VtDrawnListGetSelectedItem $dlist -byPositionList]
  373.  
  374.     VxExpandoDeleteItem $dlist $pos
  375. }
  376.  
  377. proc defaultCB {cbs} {
  378.     set dlog [keylget cbs dialog]
  379.  
  380.     set dlist [VxGetVar $dlog dlist]
  381.  
  382.     set pos [VtDrawnListGetSelectedItem $dlist -byPositionList]
  383.     VxExpandoGetRecord $dlist $pos level icon record
  384.  
  385.     if {$icon == 0} {
  386.     VxExpandoSetIcon $dlist $pos 1
  387.     addBogus $dlist $pos [expr $level + 1]
  388.     } elseif {$icon == 1} {
  389.     VxExpandoSetIcon $dlist $pos 0
  390.     VxExpandoCollapse $dlist $pos
  391.     }
  392. }
  393.  
  394. proc QuitCB {cbs} {
  395.      VtClose
  396.      exit
  397. }
  398.  
  399. set ap [VtOpen hlist]
  400.  
  401. set dlog [VtFormDialog $ap.form \
  402.       -okCallback     addCB           -okLabel         Add \
  403.       -applyCallback  QuitCB    -applyLabel     "Quit" \
  404.       -cancelCallback deleteCB      -cancelLabel       Delete ]
  405.  
  406. set dlist [VxExpandoList $dlog.list \
  407.            addCB {{STRING 20 5} {DATA} {DATA}} \
  408.            -iconList {dir.px odir.px exec.px file.px check.px} \
  409.            -rows 14 -columns 20 \
  410.            -horizontalScrollBar 1 \
  411.            -labelList {"icon" "abcdefghijklmnopqur1234567892jdjfkl"} \
  412.            -labelFormatList {{ICON 1} {STRING 20}} \
  413.            -defaultCallback defaultCB \
  414.            -rightSide FORM -bottomSide FORM ]
  415.  
  416. VtShow $dlog
  417.  
  418. VxExpandoAddItem $dlist 0 0 1 { "Hello 1" "hide" "hide"}
  419. VxExpandoAddItem $dlist 0 1 1 { "Hello 2" "hide" "hide"}
  420. VxExpandoAddItem $dlist 0 2 0 { "Hello 3" "hide" "hide"}
  421. VxExpandoAddItem $dlist 0 2 0 { "Hello 4" "hide" "hide"}
  422. VxExpandoAddItem $dlist 0 2 1 { "Hello 5" "hide" "hide"}
  423. VxExpandoAddItem $dlist 0 3 0 { "Hello 6" "hide" "hide"}
  424. VxExpandoAddItem $dlist 0 2 0 { "Hello 7" "hide" "hide"}
  425. VxExpandoAddItem $dlist 0 1 1 { "Hello 8" "hide" "hide"}
  426. VxExpandoAddItem $dlist 0 2 0 { "Hello 9" "hide" "hide"}
  427. VxExpandoAddItem $dlist 0 3 0 { "Hello a" "hide" "hide"}
  428. VxExpandoAddItem $dlist 0 1 0 { "Hello b" "hide" "hide"}
  429. VxExpandoAddItem $dlist 0 1 0 { "Hello c" "hide" "hide"}
  430. VxExpandoAddItem $dlist 0 0 0 { "Hello d" "hide" "hide"}
  431.  
  432. VxExpandoAddItem $dlist 11 1 0 { "Hello between" "hide" "hide"}
  433.  
  434. VxSetVar $dlog dlist $dlist
  435.  
  436. VtDrawnListSelectItem $dlist -position 3
  437.  
  438. VtMainLoop
  439.  
  440.  
  441.